package com.bruce.learnrecycler.kotlin.ui.utils

import android.support.v7.widget.RecyclerView
import android.support.v7.widget.helper.ItemTouchHelper

/**
 * Created by wangzhijie@wind-mobi.com on 2015/9/2.
 */
public class ItemTouchBuilder : ItemTouchHelper.Callback() {
    private var dragFlagFunc: ((RecyclerView.ViewHolder, Int, DragFlags) -> DragFlags)? = null
    private var swipeFlag: ((RecyclerView.ViewHolder, Int, SwipeFlags) -> SwipeFlags)? = null
    private var dragListener: ((RecyclerView, Int, RecyclerView.ViewHolder, RecyclerView.ViewHolder) -> Boolean)? = null
    private var swipeListener: ((viewHolder: RecyclerView.ViewHolder, Int, direction: Int) -> Unit)? = null
    var recyclerView:RecyclerView? = null
    public fun setDrag(dragFunc: (RecyclerView.ViewHolder, Int, DragFlags) -> DragFlags): ItemTouchBuilder {
        dragFlagFunc = dragFunc
        return this
    }

    public fun setSwipe(swipeFunc: (RecyclerView.ViewHolder, Int, SwipeFlags) -> SwipeFlags): ItemTouchBuilder {
        swipeFlag = swipeFunc
        return this
    }

    public fun onDrag(listener: (RecyclerView, Int, RecyclerView.ViewHolder, RecyclerView.ViewHolder) -> Boolean): ItemTouchBuilder {
        dragListener = listener
        return this
    }

    public fun onSwipe(listener: (viewHolder: RecyclerView.ViewHolder, Int, direction: Int) -> Unit): ItemTouchBuilder {
        swipeListener = listener;
        return this
    }

    public fun attachToRecyclerView(recyclerView: RecyclerView): Unit {
        ItemTouchHelper(this) attachToRecyclerView recyclerView
        this.recyclerView = recyclerView
    }



    override fun isLongPressDragEnabled(): Boolean = dragListener != null

    override fun isItemViewSwipeEnabled(): Boolean = swipeListener != null

    override fun getMovementFlags(recyclerView: RecyclerView?, viewHolder: RecyclerView.ViewHolder?): Int {
        var drag = 0
        if (dragFlagFunc != null) {
            drag = dragFlagFunc!!.invoke(viewHolder!!, recyclerView!!.getAdapter().getItemCount(), DragFlags()).dragFlags
        }
        var swipe = 0
        if (swipeFlag != null) {
            swipe = swipeFlag!!.invoke(viewHolder!!, recyclerView!!.getAdapter().getItemCount(), SwipeFlags()).swipeFlags
        }
        return ItemTouchHelper.Callback.makeMovementFlags(drag, swipe)
    }

    override fun onMove(recyclerView: RecyclerView?, viewHolder: RecyclerView.ViewHolder?, target: RecyclerView.ViewHolder?): Boolean {
        return if (recyclerView != null && viewHolder != null && target != null && dragListener != null) {
            dragListener!!(recyclerView,recyclerView.getAdapter().getItemCount(), viewHolder, target)
        } else false
    }

    override fun onSwiped(viewHolder: RecyclerView.ViewHolder?, direction: Int) {
        if (viewHolder != null && swipeListener != null) {
            swipeListener!!(viewHolder, recyclerView!!.getAdapter().getItemCount(),direction)
        }
    }
}

public class DragFlags {
    var dragFlags = 0;
    public fun canDragDown(): DragFlags {
        dragFlags = dragFlags or ItemTouchHelper.DOWN
        return this
    }

    public fun canDragUp(): DragFlags {
        dragFlags = dragFlags or ItemTouchHelper.UP
        return this
    }
}

public class SwipeFlags {
    var swipeFlags = 0;
    public fun canSwipeLeft(): SwipeFlags {
        swipeFlags = swipeFlags or ItemTouchHelper.START
        return this
    }

    public fun canSwipeRight(): SwipeFlags {
        swipeFlags = swipeFlags or ItemTouchHelper.END
        return this
    }
}